import React from 'react';
import {injectIntl, language} from './intl';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import DateRangePicker from 'Shared/components/DateRangePicker';
import {FORM_OPTIONS} from '../constants';
import WrappedSelect from '../common/WrappedSelect';
import {currency, baseDataStatus} from '../Enum';
const FormItem = Form.Item;
const currencyToList = currency.toList(language);
const baseDataStatusToList = baseDataStatus.toList(language);
class QueryPanel extends React.PureComponent {
    handleFilter = (value, name) => {
        this.props.onConditionsChange(name, value);
    };
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.onClickQuery();
    };

    onClickQuery = () => {
        this.props.querySearch();
    }
    render() {
        const {conditions} = this.props;
        const {formatMessage} = this.props.intl;
        const {brands} = this.props.initEnumData;
        return (
            <div>
                <Form className="form-standard">
                    <Row>
                        {brands.length !== 1 && <Col {...FORM_OPTIONS.col}><FormItem label={formatMessage({
                            id: 'queryPanel.form.brandId',
                            defaultMessage: '品牌'
                        })}{...FORM_OPTIONS.item}>
                            <WrappedSelect
                                allowClear
                                name="brandId"
                                value={this.props.conditions.brandId}
                                options={brands}
                                onChange={this.handleFilter}/>
                        </FormItem>
                        </Col>}
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={formatMessage({
                                    id: 'queryPanel.form.sourceCurrency',
                                    defaultMessage: '本位币'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    name="sourceCurrency"
                                    allowClear
                                    value={conditions.sourceCurrency}
                                    options={currencyToList}
                                    onChange={this.handleFilter} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={formatMessage({
                                    id: 'queryPanel.form.targetCurrency',
                                    defaultMessage: '目标币种'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    name="targetCurrency"
                                    allowClear
                                    value={conditions.targetCurrency}
                                    options={currencyToList}
                                    onChange={this.handleFilter} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={formatMessage({
                                    id: 'queryPanel.form.beginDateTime',
                                    defaultMessage: '生效日期'
                                })}
                                {...FORM_OPTIONS.item}>
                                <DateRangePicker name="beginDateTime" value={conditions.beginDateTime}
                                    onChange={this.handleFilter} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={formatMessage({
                                    id: 'queryPanel.form.endDateTime',
                                    defaultMessage: '失效日期'
                                })}
                                {...FORM_OPTIONS.item}>
                                <DateRangePicker name="endDateTime" value={conditions.endDateTime}
                                    onChange={this.handleFilter} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={formatMessage({
                                    id: 'queryPanel.form.status',
                                    defaultMessage: '状态'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    name="status"
                                    allowClear
                                    value={conditions.status}
                                    options={baseDataStatusToList}
                                    onChange={this.handleFilter} />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="operation-buttons">
                        <Col span={6}>
                            <Button
                                key="query"
                                type="primary"
                                onClick={this.onClickQuery}
                                loading={this.props.loading}>
                                {this.props.intl.formatMessage({
                                    id: 'queryPanel.btn.query',
                                    defaultMessage: '查询'
                                })}
                            </Button>
                            <Button key="clear" onClick={this.props.onClickReset}>
                                {this.props.intl.formatMessage({
                                    id: 'queryPanel.btn.reset',
                                    defaultMessage: '重置'
                                })}
                            </Button>
                        </Col>
                    </Row>
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    querySearch: PropTypes.func.isRequired,
    onClickReset: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onConditionsPressEnter: PropTypes.func.isRequired,
    intl: PropTypes.object,
};
import {connect} from 'react-redux';
import {saveQueryCondition, querySearch, resetQueryPanel} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {createSelector} from 'reselect';
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'init']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? initData.brands.map(item => ({
            text: `(${item.code})${item.name}`,
            value: item.id
        })) : [];
        return {
            brands
        };
    }
);

const mapStateToProps = state => ({
    initEnumData: getinitEnumData(state),
    conditions: getConditions(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
});


const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    onConditionsPressEnter: (value, name) => {
        dispatch(saveQueryCondition(name, value));
        dispatch(querySearch());
    },
    querySearch: () => dispatch(querySearch()),
    onClickReset: () => dispatch(resetQueryPanel()),
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(injectIntl(QueryPanel));
